package com.zxc.item.service.impl;

import com.zxc.common.enums.ShoppingEnum;
import com.zxc.common.exception.ShoppingException;
import com.zxc.common.vo.ResultVo;
import com.zxc.item.dao.TbBrandMapper;
import com.zxc.item.entity.TbBrand;
import com.zxc.item.service.TbBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TbBrandServiceImpl implements TbBrandService {

    @Autowired
    private TbBrandMapper tbBrandMapper;


    @Override
    public ResultVo<TbBrand> getAllBrand() {
        //创建一个ResultVo值对象，用于封装返回类型
        ResultVo<TbBrand> tbBrandResultVo = new ResultVo<>();

        //查询所有品牌信息
        List<TbBrand> tbBrandList = tbBrandMapper.selectAll();

        tbBrandResultVo.setData(tbBrandList);
        tbBrandResultVo.setCount(tbBrandList.size());

        //判断品牌信息列表是否为空
        if (tbBrandList.isEmpty()){
            throw new ShoppingException(ShoppingEnum.BRAND_LIST_NOT_FOUND);
        }
        return tbBrandResultVo;
    }

    @Override
    public List<TbBrand> getBrandById() {
        return null;
    }

    @Override
    @Transactional
    public void save(TbBrand tbBrand) {
        int result = tbBrandMapper.insert(tbBrand);
        if (result <= 0){
            throw new ShoppingException(ShoppingEnum.SAVE_BRAND_FAIL);
        }
    }
}
